<cfparam name="Request.doAction" type="string" default="" />


<cfswitch expression="#LCase( Trim( Request.doAction ) )#">

	<cfcase value="auth.login.view">
		<cfset getPageContext().forward( "./auth/login.cfm" ) />
	</cfcase>

	<cfcase value="auth.login.do">
		<cftry>

			<cfif StructKeyExists( Request, "EmailAddress" ) And StructKeyExists( Request, "Password" )>
				<cfset Variables.result = Application.cfc.smugAuth.withPassword( argumentCollection = Request ) />
				<cflock scope="Session" timeout="60">
					<cfset Session.user = Variables.result />
					<cfset Session.user.loggedIn = true />
				</cflock>
			<cfelseif StructKeyExists( Request, "UserID" ) And StructKeyExists( Request, "PasswordHash" )>
				<cfset Variables.result = Application.cfc.smugAuth.withHash( argumentCollection = Request ) />
				<cflock scope="Session" timeout="60">
					<cfset Session.user = Variables.result />
					<cfset Session.user.loggedIn = true />
				</cflock>
			</cfif>

			<cflock scope="Session" timeout="60">
				<cfset ArrayAppend( Session.messages.success, "Logged In!" ) />
			</cflock>
			<cflocation url="./index.cfm" addtoken="false" />

		<cfcatch type="any">
			<cflock scope="Session" timeout="60">
				<cfset ArrayAppend( Session.messages.errors, "Error Loggin In: #cfcatch.errorCode# #cfcatch.message# #cfcatch.detail#" ) />
			</cflock>
			<cflocation url="./auth/login.cfm" addtoken="false" />
		</cfcatch>
		</cftry>
	</cfcase>

	<cfcase value="auth.logout">
		<cftry>
			<cfif Session.user.loggedIn>
				<cfset Application.cfc.smugAuth.logout( SessionID = Session.user.SessionID ) />
			</cfif>

			<cflock scope="Session" timeout="60">
				<cfset ArrayAppend( Session.messages.success, "Logged Out!" ) />
			</cflock>
			<cflocation url="./index.cfm" addtoken="false" />

		<cfcatch type="any">
			<cflock scope="Session" timeout="60">
				<cfset ArrayAppend( Session.messages.errors, "Error Logging Out: #cfcatch.errorCode# #cfcatch.message# #cfcatch.detail#" ) />
			</cflock>
			<cflocation url="./index.cfm" addtoken="false" />
		</cfcatch>
		</cftry>
	</cfcase>


	<cfcase value="albums.update.view">
		<cfset Request.smugTree = Application.cfc.smugUsers.getTree( SessionID = Session.user.SessionID,
																	 AlbumsOnly = true ) />

		<cfset Request.smugCategoriesTree = Application.cfc.smugUsers.getTree( SessionID = Session.user.SessionID,
																			   ExcludeAlbums = true ) />

		<cfset Request.albumTemplates = Application..cfc.smugAlbums.getAlbumTemplates( SessionID = Session.user.SessionID ) />

		<cfset getPageContext().forward( "./albums/updateAlbums.cfm" ) />
	</cfcase>

	<cfcase value="albums.update.do">
		<cftry>

			<cfparam name="Request.AlbumTemplateID" type="integer" default="0" />
			<cfparam name="Request.catid_subcatid" type="string" default="" />
			<cfparam name="Request.action_type" type="string" default="update" />

			<cfset Variables.args = StructNew() />
			<cfset Variables.args.SessionID = Session.user.SessionID />

			<!---   Since changeSettings doesn't support AlbumTemplateID directly, if its passed in, get all the settings for the given
					AlbumTemplateID, then pass those into changeSettings --->
			<cfif Len( Request.AlbumTemplateID ) And Request.AlbumTemplateID neq 0>
				<cfset Variables.args.AlbumTemplateID = Request.AlbumTemplateID />
			</cfif>

			<cfif ListLen( Request.catid_subcatid, "-" ) eq 2>
	 			<cfset Variables.args.CategoryID = ListFirst( Request.catid_subcatid, "-" ) />
				<cfset Variables.args.SubCategoryID = ListLast( Request.catid_subcatid, "-" ) />
			</cfif>

			<cfif Request.action_type eq "copy">
				<cfset Variables.args.CFSmugImagesCFC = Application.cfc.smugImages />
			</cfif>

			<cfloop list="#Request.AlbumID#" index="Variables.currentAlbumID">
				<cfif IsValid( "integer", Variables.currentAlbumID )>
					<cfset Variables.args.AlbumID = Variables.currentAlbumID />

					<cfif Request.action_type eq "update">
						<cfset Application.cfc.smugAlbums.changeSettings( argumentCollection = Variables.args ) />
					<cfelseif Request.action_type eq "copy">
						<!--- get album title --->
						<cfset Variables.albumInfo = Application.cfc.smugAlbums.getInfo( SessionID = Variables.args.SessionID ,
																						 AlbumID = Variables.currentAlbumID ) />
						<cfset Variables.args.Title = "Copy of " & Variables.albumInfo.AlbumTitle />
						<cfset Application.cfc.smugAlbums.copy( argumentCollection = Variables.args ) />
					</cfif>
				</cfif>
			</cfloop>

			<cflock scope="Session" timeout="60">
				<cfif Request.action_type eq "update">
					<cfset ArrayAppend( Session.messages.success, "Albums Updated!" ) />
				<cfelseif Request.action_type eq "copy">
					<cfset ArrayAppend( Session.messages.success, "Albums Copied!" ) />
				</cfif>
			</cflock>
			<cflocation url="./action.cfm?doAction=albums.update.view" addtoken="false" />

		<cfcatch type="any">
			<cfdump var="#cfcatch#"><cfabort>
			<cflock scope="Session" timeout="60">
				<cfset ArrayAppend( Session.messages.errors, "Error Updating Albums: #cfcatch.errorCode# #cfcatch.message# #cfcatch.detail#" ) />
			</cflock>
			<cflocation url="./action.cfm?doAction=albums.update.view" addtoken="false" />
		</cfcatch>
		</cftry>
	</cfcase>

	<cfdefaultcase>
		<cfthrow type="#Application.config.appBaseName#" message="Unknown action to execute" detail="#Request.doAction#" />
	</cfdefaultcase>
</cfswitch>
